package com.burningtower.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.burningtower.common.Result;
import com.burningtower.entity.Resource;
import com.burningtower.entity.User;
import com.burningtower.service.IResourceService;
import com.burningtower.service.IRoleResourceService;
import com.burningtower.service.IUserRoleService;
import com.burningtower.service.IUserService;
import com.burningtower.vo.ResourceVo;
import com.burningtower.vo.TreeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 资源 前端控制器
 * </p>
 *
 * @author zhe
 * @since 2017-11-17
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private IResourceService service;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRoleResourceService roleResourceService;


    @RequestMapping("treeGrid")
    public List<ResourceVo> treeGrid(){
        List<Resource> resources = service.selectList(null);
        List<ResourceVo> resourceVos=new ArrayList<>();
        for(Resource r:resources){
            ResourceVo resourceVo=new ResourceVo();
            BeanUtils.copyProperties(r,resourceVo);
            resourceVo.setIconCls(r.getIcon());
            resourceVos.add(resourceVo);
        }
        return resourceVos;
    }
    @RequestMapping({"allTree","tree"})
    public List<TreeVo> allTree(HttpServletRequest request){
        List<TreeVo> list=new ArrayList<>();
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        User user = userService.selectOne(new EntityWrapper<>(new User(username)));
        Long[] roleIds = userRoleService.selectRoleIdByUserId(user.getId());
        for(Long roleId:roleIds){
            List<Long> resourceIds = roleResourceService.selectResourceIdListByRoleId(roleId);
            for(Long r:resourceIds){
                Resource resource = service.selectById(r);
                //如果是导航栏则跳过resourceType为1的资源
                if(request.getRequestURI().equals("/xingzhe/resource/tree")&&resource.getResourceType()!=0){
                    continue;
                }
                TreeVo vo = service.toVo(resource);
                if(request.getRequestURI().equals("/xingzhe/resource/allTree")){
                    vo.setOpenMode(null);
                }
                if(list.contains(vo)){
                    continue;
                }
                list.add(vo);
            }
        }
        return list;
    }
    @RequestMapping("allTrees")
    public List<TreeVo> allTrees(){
        List<Resource> resources = service.selectList(null);
        List<TreeVo> vos=new ArrayList<>();
        for(Resource resource:resources){
            TreeVo vo = service.toVo(resource);
            vo.setOpenMode(null);
            vos.add(vo);
        }
        return vos;
    }
    @RequestMapping("delete")
    public Result delete(Long id){
        service.deleteById(id);
        return new Result("删除成功！");
    }
    @RequestMapping("edit")
    public Result edit(Resource resource){
        service.updateById(resource);
        return new Result("编辑成功！");
    }
    @RequestMapping("add")
    public Result add(Resource resource){
        resource.setCreateTime(new Date());
        service.insert(resource);
        return new Result("添加成功");
    }
}

